package com.example.hometown_walker.util;

import com.example.hometown_walker.exception.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class RedisClient {
    private static final String ERROR_MSG_01 = "redis 'key' cannot be blank!";
    private static final String ERROR_MSG_02 = "redis increment/decrement number must grater than 0!";

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private ValueOperations<String, Object> valueOperations;
    @Resource
    private HashOperations<String, String, Object> hashOperations;
    @Resource
    private ListOperations<String, Object> listOperations;
    @Resource
    private SetOperations<String, Object> setOperations;
    @Resource
    private ZSetOperations<String, Object> zSetOperations;

    /**
     * 设置过期时间
     *
     * @param key
     * @param time
     * @return
     */
    public boolean expire(String key, long time) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
                return true;
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }

    /**
     * 根据key 获取过期时间
     *
     * @param key 键
     * @return 时间(秒) 0 永久有效
     */
    public Long getExpire(String key) {
        AssertUtil.nonBlank(key, ERROR_MSG_01);
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    /**
     * 判断key是否存在
     *
     * @param key 键
     * @return true 存在 false 不存在
     */
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 删除缓存
     *
     * @param key 键
     */
    @SuppressWarnings("unchecked")
    public void del(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                redisTemplate.delete(key[0]);
            } else {
                redisTemplate.delete(CollectionUtils.arrayToList(key));
            }
        }
    }

    ////////////////////////////// 字符串 ///////////////////////

    public Object get(String key) {
        AssertUtil.nonBlank(key, ERROR_MSG_01);
        return valueOperations.get(key);
    }

    public boolean set(String key, Object value) {
        AssertUtil.nonBlank(key, ERROR_MSG_01);
        valueOperations.set(key, value);
        return true;
    }

    public boolean set(String key, Object value, long expireTime) {
        if (expireTime > 0) {
            valueOperations.set(key, value, expireTime, TimeUnit.SECONDS);
            return true;
        } else {
            return set(key, value);
        }
    }

    public Long incr(String key, long num) {
        AssertUtil.isTrue(num > 0, ERROR_MSG_02);
        return valueOperations.increment(key, num);
    }

    public Long decr(String key, long num) {
        AssertUtil.isTrue(num > 0, ERROR_MSG_02);
        return valueOperations.decrement(key, num);
    }

    ////////////////////////////// Hash ///////////////////////

}
